package com.push.notifications;

import com.push.cache.ImageLoader;
import com.push.notifications.R;
import java.util.List;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import android.widget.ToggleButton;

 public class UI_ListServiceFirst extends ArrayAdapter<categoryItem> {
    
    private  static List<categoryItem> list;
    private   Activity context;
    boolean checkAll_flag = false;
    boolean checkItem_flag = false;
    private categoryItem model_just_click;
    private View curCheckBoxView; 
    
    public ImageLoader imageLoader;
    
    public UI_ListServiceFirst(Activity context, List<categoryItem> list) {
        super(context, R.layout.list_sevice, list);
        this.context = context;
        this.list = list;
        imageLoader=new ImageLoader(this.context.getApplicationContext());
    }
    
    static class ViewHolder {
        protected TextView text;
        protected TextView textDesc;
        protected ToggleButton checkbox;
        protected ImageView image;
        protected Button image_play;
        protected int post;
        
    }
    
    public Activity getParrentContext()
    {
    	return context;
    }
    public void setJustClick(categoryItem model){
    	model_just_click = model;
    }
    
    public categoryItem getJustClick(){
    	return model_just_click;
    }
    
    public void updateStatus(categoryItem model)
    {
    	ViewHolder viewHolder = null;
    	viewHolder = new ViewHolder();
    	viewHolder.checkbox = (ToggleButton) curCheckBoxView.findViewById(R.id.checkBox1);    
        viewHolder.checkbox.setChecked(model.isSelected());
        list.set(model.getPos(), model);
        
    }
    @Override
    public View getView( int position, View convertView, ViewGroup parent) {
        
         ViewHolder viewHolder = null;
        
        if (convertView == null && list != null) {
            LayoutInflater inflator = context.getLayoutInflater();
            Log.e("View", "convertView == null:-------------------------");
            convertView = inflator.inflate(R.layout.list_sevice, null);
            
            convertView.setClickable(true);
            
            convertView.setOnClickListener(((NotificationActivity)context).myClickListener);
            
            viewHolder = new ViewHolder();
            categoryItem model1 =list.get(position);
            
            viewHolder.text = (TextView) convertView.findViewById(R.id.label);
            viewHolder.text.setText(model1.name) ;
            
            
            viewHolder.textDesc = (TextView) convertView.findViewById(R.id.textView1);
            viewHolder.textDesc.setText(model1.getDesc()) ;
            
            viewHolder.image = (ImageView) convertView.findViewById(R.id.logo);
            viewHolder.image_play = (Button) convertView.findViewById(R.id.gallery1);
            //viewHolder.image.setImageResource(model1.id_image);
            imageLoader.DisplayImage(model1.s_image, viewHolder.image);
            
            viewHolder.checkbox = (ToggleButton) convertView.findViewById(R.id.checkBox1);

            convertView.setTag(viewHolder);
            convertView.setTag(R.id.label, viewHolder.text);
            convertView.setTag(R.id.logo, viewHolder.image);
            convertView.setTag(R.id.checkBox1, viewHolder.checkbox);
            convertView.setTag(R.id.gallery1, viewHolder.image_play);
            
            /** click on for register services **/
            viewHolder.checkbox.setOnClickListener( new OnClickListener() {

                @SuppressWarnings("deprecation")
				public void onClick(View checkbox_view) {
                	Log.e("button", "OnClickListener: checkbox" );
                	int post_checkbox = (Integer) checkbox_view.getTag();  //lay vi tri cua check box
                	
    				categoryItem model_checkbox =list.get(post_checkbox);         // lay ra du lieu Model o vi tri do
    				curCheckBoxView = checkbox_view;
    				setJustClick(model_checkbox);
    				if(!CUtilsFunction.checkConnect(context))
    		        {
    					context.showDialog(Definitions.DIALOG_ALERT);
    		        }
    				else
    				{
    					((NotificationActivity)context).AddRemoveService();
    				}
    				   				
                }
            });
            
            /** click on show sub-news **/
            viewHolder.image_play.setOnClickListener( new OnClickListener() {

				public void onClick(View image_play_view) {
                	Log.e("button", "OnClickListener: image_play" );
                	int post_button = (Integer) image_play_view.getTag();  //lay vi tri cua check box
                	categoryItem model_click =list.get(post_button);                	
                	((NotificationActivity)context).startDialogSubNews(model_click);	
    				   				
                }

            });
            
            } else {
            Log.e("View", "convertView !!!= null:-------------------------");
            viewHolder = (ViewHolder) convertView.getTag();
        }
        categoryItem model2 =list.get(position);
        viewHolder.checkbox.setTag(position); // This line is important.
        viewHolder.image_play.setTag(position); // This line is important.
        viewHolder.checkbox.setChecked(model2.isSelected());
        viewHolder.post=position;
        imageLoader.DisplayImage(model2.s_image, viewHolder.image);
        viewHolder.text.setText(model2.name);
        if(model2.getCommand().equalsIgnoreCase("-1"))
        {
        	viewHolder.checkbox.setVisibility(View.INVISIBLE);
        	viewHolder.image_play.setVisibility(View.VISIBLE);
        }
        else
        {
        	viewHolder.checkbox.setVisibility(View.VISIBLE);
        	viewHolder.image_play.setVisibility(View.INVISIBLE);
        }
        //viewHolder.image.setImageResource(model2.id_image);
        //Uri url=null;
        //viewHolder.image.setImageURI(url);
        return convertView;
    }
    	
} 
